Page({
  data: {
    exams: [], // 存储试卷列表  
    hasMore: true, // 用于控制是否还有更多数据  
    currentPage: 1, // 当前请求的页码  
    pageSize: 10, // 每页10个试卷  
    isManaging: false, // 管理模式  
    selectedExams: [] // 保存选中的试卷ID  
  },

  onLoad() {
    this.fetchExams(); // 页面加载时获取试卷数据  
  },

  onPullDownRefresh() {
    this.setData({
      exams: [],
      hasMore: true,
      currentPage: 1
    }, () => {
      this.fetchExams(); // 下拉刷新时重新请求数据  
    });
  },

  fetchExams() {
    const {
      currentPage,
      pageSize
    } = this.data;
    const app = getApp();
    wx.request({
      method: 'POST',
      url:`${app.globalData.baseUrl}/paper/paper-page?page=${currentPage}&pageSize=${pageSize}`,
      header: {
        'authentication': 'eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3MzI1NTg3MTcsInVzZXJJZCI6MTgyNzk5OTgyMTczMTA5ODYyNn0.SxS7ENOxgqgIh8AAn_Sw84R_zvFA99_khzsVNCDShLk'
      },
      success: (res) => {
        if (res.data.code === 1) {
          const newExams = res.data.data.records;
          this.setData({
            exams: [...this.data.exams, ...newExams],
            hasMore: newExams.length === pageSize // 判断是否还有更多数据  
          });

        } else {
          wx.showToast({
            title: '数据获取失败',
            icon: 'none'
          });
          //  console.log('获取到的试卷数据:', res.data.data);  // 打印获取的数据
        }
      },
      fail: () => {
        wx.showToast({
          title: '请求失败，请重试',
          icon: 'none'
        });
      },
      complete: () => {
        wx.stopPullDownRefresh(); // 结束下拉刷新  
      }
    });
  },

  loadMore() {
    if (this.data.hasMore) {
      this.setData({
        currentPage: this.data.currentPage + 1
      }, () => {
        this.fetchExams(); // 加载更多数据  
      });
    }
  },

  startExam(event) {
    const examId = event.currentTarget.dataset.examId; // 获取试卷ID  

    // 弹出提示框让用户确认  
    wx.showModal({
      title: '提示',
      content: '您确定要开始答题吗？',
      success: (res) => {
        if (res.confirm) {
          // 用户点击确定，发送请求进入答题页面  
          wx.navigateTo({
            url: `/pages/historyjuan/historyjuan?examId=${examId}` // 跳转到答题卡页面  
          });
        }
      }
    });
  },

  deleteExam(event) {
    // console.log('事件对象:', event); // 查看事件对象  
    const examId = event.currentTarget.dataset.examId; // 获取试卷ID  
    //console.log('试卷ID:', examId); // 输出ID以确认  

    if (!examId) {
      wx.showToast({
        title: '试卷ID未找到',
        icon: 'none'
      });
      return;
    }
    
    // 弹出提示框确认删除  
    wx.showModal({
      title: '确认删除',
      content: '您确定要删除这份试卷吗？',
      success: (res) => {
        if (res.confirm) {
          // 用户确认删除，发送删除请求  \
          const app = getApp();
          wx.request({
            url: `${app.globalData.baseUrl}/paper/delete-paper`,
            method: 'DELETE',
            data: {
              id: examId
            }, // 确保使用试卷的ID  
            header: {
              'authentication': 'eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3MzI1NTg3MTcsInVzZXJJZCI6MTgyNzk5OTgyMTczMTA5ODYyNn0.SxS7ENOxgqgIh8AAn_Sw84R_zvFA99_khzsVNCDShLk'
            },
            success: (res) => {
              //  console.log('删除响应:', res);调试输出  
              if (res.data.code === 1) {
                // 更新试卷列表  
                const remainingExams = this.data.exams.filter(exam => exam.id !== examId);
                this.setData({
                  exams: remainingExams
                });
                wx.showToast({
                  title: '删除成功',
                  icon: 'success'
                });
              } else {
                wx.showToast({
                  title: '删除失败',
                  icon: 'none'
                });
              }
            },
            fail: () => {
              wx.showToast({
                title: '请求失败，请重试',
                icon: 'none'
              });
            }
          });
        }
      }
    });
  },
});